<template>
  <el-dialog :title="title" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form ref="formRef" :model="form" label-width="200px" v-if="open_type != 'psw'">
      <el-form-item label="FAQ" required prop="question">
        <el-input class="max-w460" v-model="form.question" placeholder="请输入问题"></el-input>
      </el-form-item>
      <el-form-item label="FAQ名称" prop="answer">
        <el-input type="textarea" class="max-w460" :rows="6" v-model="form.answer" placeholder="请输入答案"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="order">
        <el-input-number class="max-w460" v-model="form.order" placeholder="请输入排序"></el-input-number>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="form.is_active" :active-value="1" :inactive-value="0" active-text="启用"
          inactive-text="禁用"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { addProblemApi, updateProblemApi } from "@/api/babycar";
import { addApi, editApi } from "@/api/carfilm/faq";
import { ElMessage } from "element-plus";
import { ref } from "vue";
const title = ref("编辑FAQ");
const dialogVisible = ref(false);
const form = ref({
  id: 0,
  question: "",
  answer: "",
  is_active: 1,
  order: 0,
});
const formRef = ref<any>(null);
const loading = ref(false);
const dialogFormVisible = () => {
  loading.value = false;
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (type: string, row?: any) => {
  open_type.value = type;
  if (type == "edit") {
    title.value = "编辑FAQ";
    form.value = {
      id: row.id,
      question: row.question,
      answer: row.answer,
      is_active: row.is_active,
      order: row.order,
    };
  } else if (type == "add") {
    title.value = "添加FAQ";
    form.value = {
      id: 0,
      question: "",
      answer: "",
      is_active: 1,
      order: 0,
    };
  }
  dialogVisible.value = true;
};
const emits = defineEmits(["doneSave"]);
const onSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (open_type.value == "add") {
        addApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "添加成功",
              type: "success",
            });
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else if (open_type.value == "edit") {
        editApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      }
    }
  });
};
/*取消*/
const cancelFunc = () => {
  dialogFormVisible();
};
defineExpose({ open });
</script>

<style></style>
